Micron Document
Livres et Wikis | Archives | Info


Java syntax
part 13/23 Β· 86.1 KB total
layout: Wide Β· Narrow Β· Centered
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
String bar = foo.toUpperCase();

Accessing a static class member
Static members are accessed by using the name of the class or any other type. This does not require the creation of a class instance. Static members are declared using the static modifier.

public class Foo {
public static void doSomething() {
}
}
// Calling the static method
Foo.doSomething();

Modifiers

Modifiers are keywords used to modify declarations of types and type members. Most notably there is a sub-group containing the access modifiers.

β€’ abstract - Specifies that a class only serves as a base class and cannot be instantiated.
β€’ static - Used only for member classes, specifies that the member class does not belong to a specific instance of the containing class.
β€’ final - Classes marked as final cannot be extended from and cannot have any subclasses.
β€’ strictfp - Specifies that all floating-point operations must be carried out conforming to IEEE 754 and forbids using enhanced precision to store intermediate results.

Abstract class

By default, all methods in all classes are concrete, unless the abstract keyword is used. An abstract class may include abstract methods, which have no implementation. By default, all methods in all interfaces are abstract, unless the default keyword is used. The default keyword can be used to specify a concrete method in an interface.

//By default, all methods in all classes are concrete, unless the abstract keyword is used.
public abstract class Demo {
// An abstract class may include abstract methods, which have no implementation.
public abstract int sum(int x, int y);
// An abstract class may also include concrete methods.
public int product(int x, int y) {
return x*y;
}
}
//By default, all methods in all interfaces are abstract, unless the default keyword is used.
interface DemoInterface {
int getLength(); //The abstract keyword can be used here, though is completely useless
//The default keyword can be used in this context to specify a concrete method in an interface
default int product(int x, int y) {
return x * y;
}
}

Final class

A final class cannot be subclassed. As doing this can confer security and efficiency benefits, many of the Java standard library classes are final, such as java.lang.System and java.lang.String.

Example:

public final class MyFinalClass {...}
public class ThisIsWrong extends MyFinalClass {...} // forbidden

Access modifiers

The access modifiers, or inheritance modifiers, set the accessibility of classes, methods, and other members. Members marked as public can be reached from anywhere. If a class or its member does not have any modifiers, default access is assumed.

public class Foo {
int go() {
return 0;
}
private class Bar {
}
}

The following table shows whether code within a class has access to the class or method depending on the accessing class location and the modifier for the accessed class or class member:

| Modifier | Same class or nested class | Other class inside the same package |
|---|---|---|
| private | yes | no |
| default (package private) | yes | yes |
| protected | yes | yes |
| public | yes | yes |

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────